package com.jin.dto.validators;

import com.jin.dto.PersonDTO;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

//自定义注解关联的类,要实现一个泛型的接口里面的参数第一个是注解的名字，第二个是那个类用到了这个注解这个类，字段，方法的类型
public class PasswordValidator implements ConstraintValidator<PasswordEqual, PersonDTO> {
    private int max;
    private int min;

    @Override
    public void initialize(PasswordEqual constraintAnnotation) {
        //获取注解里面的参数
         this.min = constraintAnnotation.min();
         this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(PersonDTO personDTO, ConstraintValidatorContext constraintValidatorContext) {

        String p1 = personDTO.getPassword1();
        String p2 = personDTO.getPassword2();

        if ((min<=p1.length()&&p1.length()<=max)&&(min<=p2.length()&&p2.length()<=max)){
            boolean match = p1.equals(p2);
            return match;
        }else {
            return false;
        }
    }
}
